<template>
  <div class="app-container">
    <div style="display: flex;margin-bottom: 20px;">
      <el-button size="small" type="primary" plain>服务商账单</el-button>
      <el-button size="small" plain>账单列表</el-button>
    </div>

    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="45px">
      <el-form-item label="时间:" prop="yewubumen">
        <el-date-picker v-model="value1" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-button size="small" @click="verificationShow=true" plain>详情</el-button>

    <el-table v-loading="loading" :data="demoList" style="width: 100%">
      <el-table-column label="服务商" prop="applyNum" />
      <el-table-column label="财政收入" prop="applyNum" />
      <el-table-column label="操作" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--本案例提交申请前有做会签参与人选择的操作，因此不引入 ApplyBefore 子组件-->
          <el-button v-show="!scope.row.instanceId" size="mini" type="text" icon="el-icon-check" @click="handleApplyView(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <el-dialog title="单位名称" :visible.sync="verificationShow" width="90%" append-to-body>
      <el-table v-loading="loading" :data="demoList" style="width: 100%">
        <el-table-column label="账号单" prop="applyNum" />
        <el-table-column label="总金额" prop="applyNum" />
        <el-table-column label="所属资源池" prop="applyNum" />
        <el-table-column label="服务提供商" prop="applyNum" />
      </el-table>
    </el-dialog>


  </div>
</template>

<script>
  import {
    listDemo,
    getDemo,
    delDemo,
    addDemo,
    updateDemo,
    exportDemo
  } from "@/api/example/demo";
  import {
    listUser
  } from "@/api/system/user";
  import request from '@/utils/request'

  export default {
    name: "list",
    components: {

    },
    data() {
      return {
        // 遮罩层
        loading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 示例Demo表格数据
        demoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 是否显示流程图
        showProcess: false,
        // 是否显示详情
        detailsShow: false,
        // 是否显示申请
        addedShow: false,
        // 是否显示审核
        examineShow: false,
        // 是否显示填写审核
        examineSubmitShow: false,
        examineShow2: false,
        // 是否显示填写审核
        examineSubmitShow2: false,
        // 是否显示用户确认
        userConfirmShow: false,
        // 是否显示用户确认填写意见
        userConfirmSubmitShow: false,
        // 是否显示监管平台分发工单
        distributeShow: false,
        // 是否显示监管平台分发工单选择对象
        distributeSubmitShow: false,
        // 是否显示服务商分配资源
        serviceConfirmShow: false,
        // 是否显示服务商确认填写意见
        serviceConfirmSubmitShow: false,
        // 是否显示资源核验
        verificationShow: false,
        // 是否显示资源核验填写意见
        verificationSubmitShow: false,
        // 申请步骤
        active: 1,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          yewubumen: null,
          xtmc: null,
          status: '',
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          applyNum: [{
            required: true,
            message: "申请编号不能为空",
            trigger: "blur"
          }],
          highVoltage: [{
            required: true,
            message: "是否高压停电不能为空",
            trigger: "blur"
          }],
        },
        requestMapping: '/example/demo',

        selectedRow: {},
        selectUserForm: {},
        showSelectUser: false,
        selectUserRules: {
          userOptions: [{
            required: true,
            message: '参与人不能为空',
            trigger: 'blur'
          }, ],
        },
        userOptions: [],
        userNames: [],
      };
    },
    created() {
      // this.getList();
    },
    methods: {
      /** 查询示例Demo列表 */
      getList() {
        this.loading = true;
        listDemo(this.queryParams).then(response => {
          this.demoList = response.rows;
          this.total = response.total;
          this.loading = false;
        }).then(() => {});
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          applyNum: null,
          highVoltage: "0",
          attachment: null,
          applyUserId: null,
          applyUserName: null,
          applyTime: null,
          instanceId: null,
          delFlag: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加示例Demo";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getDemo(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改示例Demo";
        });
      },
      /** 提交按钮 */
      submitForm() {
        return
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateDemo(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addDemo(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除示例Demo编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDemo(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有示例Demo数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportDemo(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      },
      handleApply: function() {
        if (this.userNames.length === 0) {
          this.msgError('请先选择会签参与人');
          return;
        }
        const selectedRow = this.selectedRow;
        const requestMapping = this.requestMapping;
        const userNames = this.userNames.toString();
        this.$confirm('是否提交ID为"' + selectedRow.id + '"的申请单据?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return request({
            url: requestMapping + '/submitApply/' + selectedRow.id,
            method: 'post',
            params: {
              "variablesStr": {
                "gytd": selectedRow.highVoltage,
                "users": userNames
              }
            },
          });
        }).then(() => {
          this.getList();
          this.msgSuccess("申请成功");
          this.userNames = [];
          this.showSelectUser = false;
        })
      },

      handleApplyView(row) {
        this.selectedRow = row;
        this.showSelectUser = true;
        this.getUserList();
      },

      /** 查询用户列表 */
      getUserList() {
        listUser().then(response => {
          this.userOptions = response.rows;
        });
      },

    }
  };
</script>
<style>
  .el-step__description {
    margin: 5px 0 14px 0 !important;
  }
</style>
